package cloud.maque.admin.controller;

import cloud.maque.admin.annotations.TenantId;
import cloud.maque.admin.annotations.TenantQuery;
import cloud.maque.admin.domain.entity.MqDepartment;
import cloud.maque.admin.domain.vo.MqDepartmentVo;
import cloud.maque.admin.service.MqDepartmentService;
import cloud.maque.framework.common.request.PaginateDataResquest;
import cloud.maque.framework.common.response.ResponseBodyVo;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zhuyf
 * @date 2023/3/17 20:57
 */
@RestController
@RequestMapping(value = "/dept")
@Api(tags = "运维-部门接口")
public class DepartmentController {

    @Autowired
    MqDepartmentService departmentService;

    @PostMapping("/save")
    @ApiOperation(value = "新增或修改部门")
    @TenantQuery
    public ResponseBodyVo<MqDepartment> Save(@RequestBody @ApiParam("部门内容") MqDepartment department) {
        department = departmentService.saveDept(department);
        return ResponseBodyVo.Success(department);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除部门")
    @TenantQuery
    public ResponseBodyVo<Boolean> Delete(@PathVariable("id") @ApiParam("部门ID") String deptId) {
        return ResponseBodyVo.Success(departmentService.delete(deptId));
    }

    @PostMapping("/move")
    @ApiOperation(value = "移动部门")
    public ResponseBodyVo<Boolean> Move(@RequestBody @ApiParam("部门ID") String deptId) {
        //todo: 移动部门
        return ResponseBodyVo.Success();
    }

    @PostMapping("/page")
    @ApiOperation(value = "部门查询")
    @TenantQuery
    public ResponseBodyVo<PageInfo<MqDepartment>> page(@RequestBody @ApiParam("查询内容") PaginateDataResquest<MqDepartment> pageReq) {
        return ResponseBodyVo.Success(departmentService.page(pageReq));
    }

    /**
     * @param parentId
     * @param tenantId 不用传值，为当前用户的租户ID
     * @return
     */
    @GetMapping("/tree/{parentId}")
    @ApiOperation(value = "部门结构树")
    @TenantQuery
    public ResponseBodyVo<List<MqDepartment>> Tree(@PathVariable("parentId") @ApiParam("父级ID") String parentId,
                                                   @TenantId String tenantId) {
        return ResponseBodyVo.Success(departmentService.Tree(parentId, tenantId));
    }

    @GetMapping("/detail/{id}")
    @ApiOperation(value = "部门详情")
    public ResponseBodyVo<MqDepartmentVo> Detail(@PathVariable("id") @ApiParam("查询内容") String id) {
        return ResponseBodyVo.Success(departmentService.Detail(id));
    }
}
